package com.cc.openeye.dynamic.follow

import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import com.cc.openeye.constance.RequestUrlConst
import com.cc.openeye.dynamic.DynamicModel
import com.cc.openeye.dynamic.follow.entity.followResponse.BaseFollowItem
import com.cc.openeye.dynamic.follow.entity.followResponse.FollowItemStates
import com.cc.openeye.dynamic.follow.entity.followResponse.Item
import com.cc.openeye.dynamic.recommend.entity.LoadMoreStatus
import com.tencent.kuikly.compose.foundation.lazy.LazyListState
import com.tencent.kuikly.core.nvi.serialization.json.JSONObject
import com.tencent.kuikly.core.views.PlayState
import com.tencent.kuikly.core.views.VideoPlayControl

class FollowViewModel {
    private val dynamicModel by lazy {
        DynamicModel.newInstance()
    }

    val datas = mutableStateListOf<BaseFollowItem>()

    val followItemStatesList = mutableListOf<FollowItemStates>()

    private val loadMoreStatus = mutableStateOf(LoadMoreStatus.HaveMore)

    private var isLoading = false

    private val requestUrl = mutableStateOf(RequestUrlConst.FOCUS_URL)

    suspend fun requestFocusData() {
        if (loadMoreStatus.value == LoadMoreStatus.NoMore) return
        if (isLoading) return
        isLoading = true
        val response = dynamicModel.requestFocusData()
        isLoading = false
        if (response.success) {
            val itemList = response.data?.itemList ?: emptyList()
            datas.addAll(itemList)
            addFollowItemStatesList(itemList)
            requestUrl.value = response.data?.nextPageUrl ?: ""
            loadMoreStatus.value =
                if (requestUrl.value.isNotEmpty()) LoadMoreStatus.HaveMore else LoadMoreStatus.NoMore
        } else {
            loadMoreStatus.value = LoadMoreStatus.LoadErr
        }
    }

    private fun addFollowItemStatesList(followItem: List<BaseFollowItem>) {
        followItem.forEach { _ ->
            followItemStatesList.add(FollowItemStates())
        }
    }

    fun changePlayState(followItemStates: FollowItemStates, state: PlayState, extInfo: JSONObject) {
        if (followItemStates.isVideoLoading.value && state == PlayState.PLAYING) {
            followItemStates.isVideoLoading.value = false
        }
        followItemStates.videoPlayState.value = state
        followItemStates.extInfo = extInfo
    }

    fun onScroll(lazyListState: LazyListState) {
        if (lazyListState.isScrollInProgress) {
            return
        }
        if (datas.isEmpty()) return
        if (followItemStatesList.isEmpty()) return
        val centerIndex =
            lazyListState.firstVisibleItemIndex + lazyListState.layoutInfo.visibleItemsInfo.size / 2
        if (datas[centerIndex] !is Item) return
        if (followItemStatesList[centerIndex].videoPlayControl.value == VideoPlayControl.PLAY) return
        followItemStatesList.forEach {
            it.videoPlayControl.value = VideoPlayControl.STOP
            it.isVideoLoading.value = false
        }
        val followItemStates = followItemStatesList[centerIndex]
        followItemStates.videoPlayControl.value = VideoPlayControl.PLAY
        followItemStates.isVideoLoading.value = true
    }

    companion object {
        fun newInstance() = FollowViewModel()
    }

}